package deleteface.bank.jqsoft.cn.deleteface.setip;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;

import com.chad.library.adapter.base.BaseViewHolder;
import com.mixiaoxiao.smoothcompoundbutton.SmoothCompoundButton;
import com.mixiaoxiao.smoothcompoundbutton.SmoothRadioButton;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.jqsoft.bank.deleteface.R;
import deleteface.bank.jqsoft.cn.deleteface.BaseQuickAdapterEx;


//
public class SettingServerAdapter extends BaseQuickAdapterEx<SettingServerBean, BaseViewHolder> {
    public static final int TYPE_SINGLE_LINE=1;
    public static final int TYPE_MULTIPLE_LINE=2;
//    HashMap<String, SettingServerStatesBean> states = new HashMap<String, SettingServerStatesBean>();
    HashMap<String, Boolean> states = new HashMap<String, Boolean>();
    List<SettingServerStatesBean> mStates=new ArrayList<>();
    List<String> iplist=new ArrayList<>();
    List<String> termiplist=new ArrayList<>();
    List<String> adresslist=new ArrayList<>();

    List<SettingServerStatesBean> settinglist=new ArrayList<>();

    private int type=TYPE_MULTIPLE_LINE;
    private Context context;
    SettingServerBean item;
    public SettingServerAdapter(List<SettingServerBean> data, int type,Context context) {
        super(R.layout.item_setting_server_single_line, data);
        this.type = type;
        this.context=context;
    }

    public HashMap<String, Boolean> getstates(){
        return  states  ;

    }
    public List<String> getIplist(){
        return iplist;
    }

    public List<String> getTermIplist(){
        return termiplist;
    }

    public List<String> getAdresslist(){
        return adresslist;
    }

    public List<SettingServerStatesBean> getSettinglist(){
        return settinglist;
    }


    public  SettingServerBean getSettingServerBean(){
        return item;
    }
    public void SettingChoose(String ip,String termip,String address){
        states.put(ip,true);

        SettingServerStatesBean bean1=new SettingServerStatesBean();
        bean1.setIp(ip);
        bean1.setTermip(termip);
        bean1.setAddress(address);
        settinglist.clear();
        settinglist.add(bean1);
    }




    @Override
    protected void convert(final BaseViewHolder helper, final SettingServerBean item) {
      this. item=item;
        helper.setText(R.id.ip,"底片:"+ Util.trimString(item.getIp()));//getname
        helper.setText(R.id.termip, "有效期:"+Util.trimString(item.getTermip()));//getname
        helper.setText(R.id.address, Util.trimString(item.getAddress()));

        iplist.add(item.getIp());
        termiplist.add(item.getTermip());
        adresslist.add(item.getAddress());


        final SmoothRadioButton radioButton=(SmoothRadioButton) helper.getView(R.id.choose);
        radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                for (String key : states.keySet()) {
                    SettingServerStatesBean bean=new SettingServerStatesBean("","",false,"");
                    states.put(key, false);


                }
                SettingServerStatesBean bean1=new SettingServerStatesBean();
                bean1.setIp(item.getIp());
                bean1.setTermip(item.getTermip());
                bean1.setAddress(item.getAddress());
                bean1.setIscheck(radioButton.isChecked());
                states.put(item.getIp(),radioButton.isChecked());

                settinglist.clear();
                settinglist.add(bean1);

                int s=  helper.getAdapterPosition();
                SharedPreferences userSettings = context.getSharedPreferences("setting", 0);
                SharedPreferences.Editor editor = userSettings.edit();
                editor.putInt("ipposition",s);
                editor.apply();



//                states.put(String.valueOf(helper.getPosition()),radioButton.isChecked());
//                mStates.add(bean1);
                notifyDataSetChanged();
            }
        });




        boolean res = false;
        if (states.get(String.valueOf(item.getIp())) == null
                || states.get(item.getIp()) == false) {
//                || states.get(String.valueOf(helper.getPosition())) == false) {
            res = false;
//            SettingServerStatesBean bean2=new SettingServerStatesBean("",false);
            states.put(item.getIp(), false);

        } else
            res = true;


        radioButton.setChecked(res);



    }



}
